﻿using AutoMapper;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.Collections.Generic;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Read.Api.Application.Command.CommodityCommand;
using Traceability.Read.Api.Dto;

namespace Traceability.Read.Api.Application.CommandHander.CommodityHander
{
    public class GetCommodityHander : IRequestHandler<GetCommodityCommand, ApiPaging<List<CommodityDto>>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Commodity> baseRequsitory;
        private readonly IBaseRequsitory<CommodityName> baseRequsitory1;

        public GetCommodityHander(IMapper mapper, IBaseRequsitory<Commodity> baseRequsitory, IBaseRequsitory<CommodityName> baseRequsitory1)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
            this.baseRequsitory1 = baseRequsitory1;
        }

        /// <summary>
        /// 显示商品信息操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<List<CommodityDto>>> Handle(GetCommodityCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<CommodityDto>> result = new ApiPaging<List<CommodityDto>>();
            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();

            var linq = from a in baseRequsitory.GetAll()
                       join b in baseRequsitory1.GetAll() on a.CommodityNameId equals b.CommodityNameId
                       select new CommodityDto
                       {
                           CommodityId = a.CommodityId,
                           CommodityNameId = a.CommodityNameId,
                           Cover = a.Cover,
                           C_Times = a.Time,
                           Time = a.Time,
                           Permit = a.Permit,
                           Enterprise = a.Enterprise,
                           Degree = a.Degree,
                           CreateTime = a.CreateTime,
                           IsDelete = a.IsDelete,
                           ProductNameName = b.ProductNameName,
                           UpdateTime = a.UpdateTime,
                       };

            if (request.CommodityNameId != null)
            {
                linq = linq.Where(x => x.CommodityNameId == request.CommodityNameId);
            }
            if (!string.IsNullOrEmpty(request.C_Times))
            {
                linq = linq.Where(x => x.C_Times == request.C_Times);
            }
            if (!string.IsNullOrEmpty(request.Time))
            {
                linq = linq.Where(x => x.Time == request.Time);
            }
            if (!string.IsNullOrEmpty(request.Permit))
            {
                linq = linq.Where(x => x.Permit == request.Permit);
            }
            if (!string.IsNullOrEmpty(request.Enterprise))
            {
                linq = linq.Where(x => x.Enterprise == request.Enterprise);
            }
            if (request.CreateTime != null)
            {
                linq = linq.Where(x=>x.CreateTime>request.CreateTime);
            }

            result.TotalCount = linq.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            var entity = linq.OrderBy(x => x.CommodityId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = mapper.Map<List<CommodityDto>>(entity);

            return Task.FromResult(result);
        }
    }
}
